<script>
	import Vue from 'vue'
	import {
		plat
	} from 'utils/conmmon.js'
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	export default {
		computed: {
			...mapGetters(['curimg', 'curindex', 'audiolist','isplayingmusic']),
		},
		onLaunch: function() {
			console.log('App Launch')
			uni.getSystemInfo({
				success: function(e) {
					// console.log(e)
					// #ifdef H5
					if (e.windowWidth > 760) {
						uni.showModal({
							content: '请用移动端设备访问',
							showCancel: false
						});
					}
					// #endif
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif
					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif       
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
					//窗口高度
					Vue.prototype.DeviceHight = e.windowHeight;
				}
			});
			let audioPlayer = null;
			let timer = null;
			if (plat === 'H5') {
				audioPlayer = uni.createInnerAudioContext()
			} else {
				audioPlayer = uni.getBackgroundAudioManager()
			}
			//注册全局变量
			Vue.prototype.$au_player = audioPlayer;
			//播放时触发
			Vue.prototype.$au_player.onPlay(function() {
				console.log("playing");
				// Vue.prototype.cusPlay && Vue.prototype.cusPlay()
				clearInterval(timer);
				timer = setInterval(() => {
					console.log('update');
					// Vue.prototype.CusTimeUpdate()
				}, 200);
			});
			//播放完触发
			Vue.prototype.$au_player.onEnded(function() {
				// this.setIsplayingmusic(false);
				// Vue.prototype.cusEnded && Vue.prototype.cusEnded()
				this.setCurIndex(this.curindex+1);
				if(this.curindex >= this.audiolist.length){
					this.setCurIndex(0);
				}
				Vue.prototype.$au_player.src = this.sudiolist[this.curindex].url;
				Vue.prototype.$au_player.play();
				console.log(this.curindex)
				clearInterval(timer)
			});
			//报错处理
			Vue.prototype.$au_player.onError(function(err) {
				console.log('play err:' + err)
				this.setIsplayactive(false)
				clearInterval(timer)
			});
			//播放停止
			Vue.prototype.$au_player.onStop(function() {
				console.log('play stop:' + res)
				this.setIsplayactive(false)
				this.setIsplayingmusic(false);
				clearInterval(timer)
			});
			
			
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			...mapMutations(['setCurImg', 'setCurIndex', 'setAudiolist', 'setIsplayingmusic']),	
		},
	}
</script>

<style>
	/*每个页面公共css */
	page {
		overflow: auto;
	}
</style>
